<template>
  <div>
    <el-form
      :model="postForm"
      ref="postForm"
      :rules="rules"
      label-width="120px"
      label-suffix=":"
    >
      <sticky :z-index="10" :class-name="'sub-navbar ' + postForm.status">
        <el-button v-loading="loading" style="margin-left: 10px;" type="success" @click="submitForm">
          保存
        </el-button>
      </sticky>

      <div style="padding: 15px">

        <help />

        <el-row>
          <el-col :span="22">
            <el-form-item label="app_id">
              <el-input v-model.trim="postForm.app_id" />
            </el-form-item>
            <el-form-item label="app_secret">
              <el-input v-model.trim="postForm.app_secret" />
            </el-form-item>
            <el-form-item label="token">
              <el-input v-model.trim="postForm.token" />
            </el-form-item>
            <el-form-item label="aes_key">
              <el-input v-model.trim="postForm.aes_key" />
            </el-form-item>
            <el-form-item label="mch_id">
              <el-input v-model.trim="postForm.mch_id" />
              <span>商户ID</span>
            </el-form-item>
            <el-form-item label="pay_key">
              <el-input v-model.trim="postForm.pay_key" />
              <span>支付key, 如: {{ random }}, 现在的APIv2密钥就是之前的api密钥</span>
            </el-form-item>
            <el-form-item label="公众号二维码">
              <upload-image
                bucket="subscribe"
                :max="1"
                :max-width="750"
                :quality="50"
                @onSuccess="onSuccess"
                @onRemove="onRemove"
                :file-list="postForm.image"
              />
            </el-form-item>
          </el-col>
        </el-row>
      </div>

    </el-form>
  </div>
</template>

<script>
import Sticky from '@/components/Sticky'
import UploadImage from '@/components/Upload/UploadImage'
import api from '@/api'
import Help from './components/Help'
export default {
  name: 'SystemWx',
  components: {
    Sticky,
    UploadImage,
    Help
  },
  data() {
    return {
      postForm: {
        status: 'draft'
      },
      rules: {
      },
      loading: false,
      // 随机数示例
      random: ''
    }
  },
  created() {
    this.fetchData()
    this.random = this.randomString()
  },
  methods: {
    /**
     * 生成随机数
     * @param len
     * @returns {string}
     */
    randomString(len = 32) {
      len = len || 32
      /* 默认去掉了容易混淆的字符oOLl,9gq,Vv,Uu,I1 */
      const $chars = 'ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprstwxyz2345678'
      const maxPos = $chars.length
      let pwd = ''
      for (let i = 0; i < len; i++) {
        pwd += $chars.charAt(Math.floor(Math.random() * maxPos))
      }
      return pwd
    },
    async fetchData() {
      const { data } = await api.system.fetchWx()
      this.postForm = Object.assign({}, data)
      this.postForm.status = 'publisher'
      // 图片
      if (this.postForm.image) {
        const list = []
        list.push({
          id: +this.postForm.image.id,
          // 这个没有缩略图
          url: this.postForm.image.path
        })
        this.postForm.image = JSON.parse(JSON.stringify(list))
      } else {
        this.postForm.image = []
      }
    },
    submitForm() {
      this.$refs.postForm.validate(validate => {
        if (validate) {
          this.loading = true
          // 图片
          let image = 0
          if (this.postForm.image && this.postForm.image.length > 0) {
            image = this.postForm.image.map(x => x.id)[0]
          }
          api.system.saveWx({
            ...this.postForm,
            image_id: image
          }).then(response => {
            this.$notify({
              title: '成功',
              message: response.message || '保存成功',
              type: 'success',
              duration: 2000
            })
            this.loading = false
          }).catch(() => {
            this.loading = false
          })
        }
      })
    },
    /**
     * 图片上传
     */
    onSuccess(data, fileList) {
      if (data && data.id) {
        this.postForm.image.push({
          id: data.id,
          url: data.full_name
        })
        // this.submitForm()
      }
    },
    /**
     * 删除图片
     */
    onRemove(data, fileList) {
      if (data && data.id) {
        this.postForm.image = this.postForm.image.filter(x => x.id !== data.id)
        this.submitForm()
      }
    }
  }
}
</script>
